<html>
<head><meta charset="utf-8"><title>Why does `const` disallow using generic parameters · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html">Why does `const` disallow using generic parameters</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223469151"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223469151" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223469151">(Jan 21 2021 at 04:34)</a>:</h4>
<p>I just got the following compile error:</p>
<div class="codehilite"><pre><span></span><code>error[E0401]: can&#39;t use generic parameters from outer function
   --&gt; library/alloc/src/raw_vec.rs:409:49
    |
362 | impl&lt;T, A: Allocator&gt; RawVec&lt;T, A&gt; {
    |      - type parameter from outer function
...
409 |         const elem_size: usize = mem::size_of::&lt;T&gt;();
    |                                                 ^ use of generic parameter from outer function
</code></pre></div>
<p>Why is this a restriction? Changing <code>const</code> to <code>let</code> works fine.</p>



<a name="223469225"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223469225" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223469225">(Jan 21 2021 at 04:34)</a>:</h4>
<p><code>size_of</code> is explicitly a <code>const fn</code> too</p>



<a name="223469317"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223469317" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223469317">(Jan 21 2021 at 04:37)</a>:</h4>
<p>using associated constants also breaks:</p>
<div class="codehilite"><pre><span></span><code>error[E0401]: can&#39;t use generic parameters from outer function
   --&gt; library/alloc/src/raw_vec.rs:411:44
    |
364 | impl&lt;T, A: Allocator&gt; RawVec&lt;T, A&gt; {
    | ---- `Self` type implicitly declared here, by this `impl`
...
411 |         const min_non_zero_cap: usize = if Self::elem_size == 1 {
    |                                            ^^^^^^^^^^^^^^^
    |                                            |
    |                                            use of generic parameter from outer function
    |                                            use a type here instead
</code></pre></div>



<a name="223469743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223469743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223469743">(Jan 21 2021 at 04:45)</a>:</h4>
<p>i think you just can't do generic associated consts yet, but they're planned to be in the language Some Day (tm)</p>



<a name="223469756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223469756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223469756">(Jan 21 2021 at 04:45)</a>:</h4>
<p>the associated const is not generic. It only depends on a generic parameter.</p>



<a name="223469764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223469764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223469764">(Jan 21 2021 at 04:45)</a>:</h4>
<p>it has a fixed type of <code>usize</code></p>



<a name="223469888"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223469888" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223469888">(Jan 21 2021 at 04:48)</a>:</h4>
<p>it's been a restriction for a while. it's kind of annoying, since you'd often like to do something like have a <code>[u8; size_of::&lt;T&gt;()]</code>, however IIUC the limitation currently is the result of const eval isn't allowed to feed back into the type system, which is what this would allow.</p>



<a name="223470054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223470054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223470054">(Jan 21 2021 at 04:53)</a>:</h4>
<p>hmm, makes sense. that's unfortunate, I would like to statically guarentee <code>min_non_zero_cap</code> is computed at compile time</p>



<a name="223484186"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223484186" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223484186">(Jan 21 2021 at 09:15)</a>:</h4>
<p>if you make <code>min_con_zero_cap</code> an associated constant of <code>RawVec</code> it should work</p>



<a name="223484255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223484255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223484255">(Jan 21 2021 at 09:16)</a>:</h4>
<p>but this may conflict with future planned changes that may end up requiring <code>const_evaluatable_checked</code>. cc <span class="user-mention" data-user-id="216206">@lcnr</span></p>



<a name="223489411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223489411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223489411">(Jan 21 2021 at 10:12)</a>:</h4>
<p>using generic associated consts seems fine to me. They are already used too much for us to forbid them outright so no matter what we do we will have to keep allowing them.</p>



<a name="223530830"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223530830" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223530830">(Jan 21 2021 at 16:13)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> the associated constant thing is what I tried, it didn't work</p>



<a name="223531343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223531343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223531343">(Jan 21 2021 at 16:16)</a>:</h4>
<p>oh, from the indentation level I was guessing it was inside a function... brb testing something</p>



<a name="223531588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223531588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223531588">(Jan 21 2021 at 16:18)</a>:</h4>
<p>idk, I can't reproduce. Do you have your code up somwhere?</p>



<a name="223531594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223531594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223531594">(Jan 21 2021 at 16:18)</a>:</h4>
<p>Here's my test: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=d9dac0dbebe8f1ad38161efbf50a06bb">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=d9dac0dbebe8f1ad38161efbf50a06bb</a></p>



<a name="223537025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223537025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223537025">(Jan 21 2021 at 16:52)</a>:</h4>
<p>oh huh, maybe I confused myself, this works fine:</p>
<div class="codehilite" data-code-language="Diff"><pre><span></span><code><span class="gh">diff --git a/library/alloc/src/raw_vec.rs b/library/alloc/src/raw_vec.rs</span>
<span class="gh">index 36b7efc33a8..0b462e99065 100644</span>
<span class="gd">--- a/library/alloc/src/raw_vec.rs</span>
<span class="gi">+++ b/library/alloc/src/raw_vec.rs</span>
<span class="gu">@@ -114,6 +114,21 @@ pub unsafe fn from_raw_parts(ptr: *mut T, capacity: usize) -&gt; Self {</span>
 }

 impl&lt;T, A: Allocator&gt; RawVec&lt;T, A&gt; {
<span class="gi">+    // Tiny Vecs are dumb. Skip to:</span>
<span class="gi">+    // - 8 if the element size is 1, because any heap allocators is likely</span>
<span class="gi">+    //   to round up a request of less than 8 bytes to at least 8 bytes.</span>
<span class="gi">+    // - 4 if elements are moderate-sized (&lt;= 1 KiB).</span>
<span class="gi">+    // - 1 otherwise, to avoid wasting too much space for very short Vecs.</span>
<span class="gi">+    // Note that `min_non_zero_cap` is computed statically.</span>
<span class="gi">+    const elem_size: usize = mem::size_of::&lt;T&gt;();</span>
<span class="gi">+    const min_non_zero_cap: usize = if Self::elem_size == 1 {</span>
<span class="gi">+        8</span>
<span class="gi">+    } else if Self::elem_size &lt;= 1024 {</span>
<span class="gi">+        4</span>
<span class="gi">+    } else {</span>
<span class="gi">+        1</span>
<span class="gi">+    };</span>
<span class="gi">+</span>
     /// Like `new`, but parameterized over the choice of allocator for
     /// the returned `RawVec`.
     #[rustc_allow_const_fn_unstable(const_fn)]
</code></pre></div>



<a name="223538044"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223538044" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223538044">(Jan 21 2021 at 16:59)</a>:</h4>
<p>opened <a href="https://github.com/rust-lang/rust/pull/81242">https://github.com/rust-lang/rust/pull/81242</a></p>



<a name="223697814"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Why%20does%20%60const%60%20disallow%20using%20generic%20parameters/near/223697814" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Why.20does.20.60const.60.20disallow.20using.20generic.20parameters.html#223697814">(Jan 22 2021 at 20:48)</a>:</h4>
<p>For what it's worth, the original issue is a limitation that is intended to be lifted: <a href="https://github.com/rust-lang/rust/issues/61414">https://github.com/rust-lang/rust/issues/61414</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>